package com.shigu.item.service;

import com.opentae.data.mall.beans.*;
import com.opentae.data.mall.examples.MallSkuExample;
import com.opentae.data.mall.interfaces.MallAdvertisingMapper;
import com.opentae.data.mall.interfaces.MallItemMapper;
import com.opentae.data.mall.interfaces.MallSkuMapper;
import com.opentae.data.mall.interfaces.UserInviteInfoMapper;
import com.shigu.PageResponse;
import com.shigu.account.api.VipManagerService;
import com.shigu.account.enums.VipLevel;
import com.shigu.constant.enums.PageUri;
import com.shigu.constant.enums.VipConstant;
import com.shigu.constant.service.SystemConstantService;
import com.shigu.exceptions.WapException;
import com.shigu.item.enums.ImgRule;
import com.shigu.item.vo.GoodsVipInfoVo;
import com.shigu.advert.vo.IndexAdvert;
import com.shigu.item.vo.ItemInfoResponse;
import com.shigu.item.vo.ItemSkuResponse;
import com.shigu.util.MoneyUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ItemWapService {
    @Autowired
    MallItemMapper mallItemMapper;
    @Autowired
    MallAdvertisingMapper mallAdvertisingMapper;
    @Autowired
    MallSkuMapper mallSkuMapper;

    @Resource(name = "vipManagerServiceImpl")
    VipManagerService vipManagerService;
    @Autowired
    SystemConstantService systemConstantService;

    @Autowired
    UserInviteInfoMapper userInviteInfoMapper;

    /**
     *　商品信息
     * @author 郭尔
     * @date 2019/1/10 13:30
     */
    public ItemInfoResponse itemInfo(Long itemId) throws WapException {
        ItemInfoResponse response = new ItemInfoResponse();
        MallItem item = mallItemMapper.selectByPrimaryKey(itemId);
        if(item==null || !item.getOnsale()){
            throw new WapException("此商品已下架或不存在");
        }
        response.setTitle(item.getTitle());
        response.setPrice(MoneyUtil.formatPriceMoney(item.getPrice()));
        response.setDiscount(MoneyUtil.formatPriceMoney(item.getDiscount()));
        response.setSales(item.getSales()+item.getFakeSales());
        response.setParams(item.getProductParam());
        String[] imgs = item.getImages().split(",");
        for (int i = 0; i <imgs.length ; i++) {
            imgs[i]+= ImgRule.rule2.getText();
        }
        response.setImgs(Arrays.asList(imgs));

        if (StringUtils.isNotEmpty(item.getDescription())){
            response.setDetails(Arrays.asList(item.getDescription().split(",")));
        }
        response.setItemCommissionAccount(item.getReward()+"");
        return response.success();
    }

    /**
     * @author 郭尔
     * @date 2019/1/10 13:54
     */
    public ItemSkuResponse itemSku(Long itemId){
        ItemSkuResponse response = new ItemSkuResponse();
        Map<String,Long> map = new HashMap<>();
        response.setSkuJson(mallItemMapper.selSkus(itemId));
        MallSkuExample example = new MallSkuExample();
        example.createCriteria().andItemIdEqualTo(itemId);
        List<MallSku> list = mallSkuMapper.selectByExample(example);
        for (MallSku sku : list) {
            map.put(sku.getSkuValue(),sku.getSkuId());
        }
        response.setSku(map);
        return response.success();
    }
    /**
     *
     * @description 查询商品折扣及价格
     * @author 郭尔
     * @date 2019-06-10 14:01
     */
    public GoodsVipInfoVo vipItemInfo(Long itemId, Long mallUserId,String uri){
        MallItem item = mallItemMapper.selectByPrimaryKey(itemId);

        //默认查安卓的
        Map<String, String> constant = systemConstantService.selPageConstant(uri);
        Integer  discount = Integer.valueOf(constant.get(VipConstant.staffDiscount.toString()));
        Double rate = 0D;

        VipLevel level = VipLevel.STAFF;

        if(mallUserId != null){
            UserInviteInfo info = userInviteInfoMapper.selectByPrimaryKey(mallUserId);
            if(info != null && info.getVipLevel() != VipLevel.COMMON.getValue()){
                discount = vipManagerService.vipDiscount(mallUserId,uri);
                level = VipLevel.selByValue(info.getVipLevel());
                rate = vipManagerService.userRate(mallUserId,uri);
            }
        }

        GoodsVipInfoVo vo = new GoodsVipInfoVo();
        vo.setDutiesName(level.getTitle());
        vo.setVipDiscount(MoneyUtil.formatPriceMoney(item.getDiscount() * discount/100));
        vo.setVipDiscountNum(Double.valueOf(discount)/10 +"");
        vo.setItemCommissionAccount(MoneyUtil.formatPriceMoney(MoneyUtil.multipy(item.getDiscount(),rate)));
        return vo.success();
    }

}  
